/*
 * Author: Jeff Liu(jebberwocky@gmail.com)
 * Date: Sep 5, 2010 7:29:33 PM
 * 
 * License: MIT
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
 * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package net.citiport.ggltranslate.ggl;

import java.io.UnsupportedEncodingException;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import net.citiport.ggltranslate.api.TranslatedTextParser;
import net.citiport.ggltranslate.api.DetectedLangParser;
import net.citiport.ggltranslate.core.Const;
import net.citiport.ggltranslate.core.LangConst;
import net.citiport.ggltranslate.event.GglTranslateEvent;
import net.citiport.ggltranslate.event.GglTranslateListener;
import net.citiport.ggltranslate.impl.JsonDetectParser;
import net.citiport.ggltranslate.impl.JsonTextParser;
import net.citiport.ggltranslate.util.DataUtil;

public class GoogleTranslator {
	
	private List _listeners = new LinkedList();
	
	protected void fireEvent(TranslatedPost post, int type){
		final GglTranslateEvent evt = new GglTranslateEvent(post, type);
		for(Iterator it = _listeners.iterator(); it.hasNext();)
			((GglTranslateListener)it.next()).onTranslated(evt);
	}
	
	public void addGglTranslateListener(GglTranslateListener l){
		_listeners.add(l);
	}
	
	public void removeGglTranslateListener(GglTranslateListener l){
		_listeners.remove(l);
	}
	
	public GoogleTranslator() {
		ttparser = new JsonTextParser();
		dlparser = new JsonDetectParser();
	}

	private TranslatedTextParser ttparser;
	
	public void setTtparser(TranslatedTextParser ttparser) {
		this.ttparser = ttparser;
	}
	
	private DetectedLangParser dlparser;
	
	public void setDlparser(DetectedLangParser dlparser) {
		this.dlparser = dlparser;
	}
	
	public static String getTextCharset(){
		return Const.CHARSET;
	}
	
	private Boolean isResultReliable(DetectedLang dl){
		if(dl ==null)
			return false;
		return dl.responseData.isReliable;
	}
	
	public  TranslatedPost GetTranslatedPost(String input, LangConst fromLang, LangConst toLang){
		if(DataUtil.IsEmptyString(input))
			return Const.EMPTY_POST;
		String detectLang = null;
		DetectedLang dl = null;
		try {
			String json = GoogleLangAPI.DetectLang(input);
			dl = this.dlparser.parse(json);
			if(dl!=null)
				detectLang  = dl.responseData.language;
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(detectLang != null && this.isResultReliable(dl)){
			detectLang = detectLang.toLowerCase().trim();
		}else{
			detectLang = fromLang.toString().toLowerCase().trim();
		}
		String resultText = null;
		String rjson;
		try {
			rjson = GoogleLangAPI.TranslateText(input, detectLang + Const.GOOGLE_LANG_SEPARATOR + toLang);
			TranslatedText tt = ttparser.parse(rjson);
			if(tt !=null)
				resultText = tt.responseData.translatedText;
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		TranslatedPost tp = null;
		if(resultText == null)
			return null;
		else
			{
				tp = new TranslatedPost(resultText, false, false, toLang.toString());
				fireEvent(tp, GglTranslateEvent.CONTENTS_TRANSLATED);
				return tp;
			}
		}
	
}
